package com.wmh.baseservice.admin.controller;

import com.alibaba.fastjson2.JSONObject;
import com.wmh.baseservice.admin.service.AdInfoService;
import com.wmh.baseservice.common.utils.SignDecodeUtil;
import com.wmh.baseservice.common.utils.encrypt.RSAUtil;
import com.wmh.baseservice.common.utils.returnbody.Code;
import com.wmh.baseservice.common.utils.returnbody.ReturnBody;
import com.wmh.baseservice.system.service.SysGoodsIdService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;


@Api(tags = "管理员-公开接口")
@RestController
@RequestMapping("admin/public")
public class AdPublicController {

    @Resource
    private AdInfoService adInfoService;
    @Resource
    private SysGoodsIdService sysGoodsIdService;
    @Resource
    private HttpServletRequest request;


    @ApiOperation("登录验证")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "account", value = "账号", required = true, example = "PtivnlNFS8UOjbkUyQ6G6n49+c9xIT8npUwsEPpzBNFm0Pt/BVeZkrY6JSf8Rx71ks359U+0Q8d7Iob/P6G9TvWgq0c0SroSzOpsJy6+E7C4Yl12VoorHNE6EImdmPrEdxiEv0ac7HruYP4Nk+D9BIxpf2T0UgDQduFrOkNRWBo="),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "password", value = "密码", required = true, example = "DLUsX3SZ0otbA5J+XE4VrG9K3f2SiLWnyHvSYrQ5CFAkJoFg0AbHKbZnJJsOmSyWwezQvQRWpnYldXmfTrsCFLCPM1RbSgB97f4Wsg0W94+URsPGRLCzRjQlZaGze/uy/DT996rgAskjGh2/1Vy8NE1NGcoE7XjWW+W+JjIGckc="),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "verifyCode", value = "图片验证码", required = true, example = "1234"),
            @ApiImplicitParam(paramType = "query", dataTypeClass = String.class, name = "verifySign", value = "图片验证码密钥", required = true, example = "bOENDfoXAhyXyi8wZ/FsfQxWso6hhErukzPz+s3xQq4=")
    })
    @PostMapping("authority")
    public String authority(String account, String password, String verifyCode, String verifySign) throws Exception {
        password = RSAUtil.decryptByPrivateKey(password);
        account = RSAUtil.decryptByPrivateKey(account);
        //验证码验证
        JSONObject verifyResult = SignDecodeUtil.codeMatches(verifyCode, verifySign, 1000000);
        if (!verifyResult.getBoolean("isSuccess")) {
            return ReturnBody.error(Code.IMAGE_CODE_ERROR, verifyResult.getString("errorMessage"));
        }
        return adInfoService.authority(account, password);
    }



}
